<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/21
 * Time: 上午11:44
 */

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Teenager;
use App\Http\Model\User;

class TeenagerServices extends CommonServices
{
    private $serviceMsg;
    protected $user;
    protected $teenager;
    protected $passwordHash = 'kk-audio';

    public function __construct()
    {
        $this->serviceMsg = new MsgServices();
        $this->user = new User();
        $this->teenager = new Teenager();
    }

    public function setLock(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('user_id', '用户id', 'int', true, '用户id不能为空');
            $this->I('password', '密码', 'int', true, '密码不能为空');

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            $pwd = $this->requestData['password'];
            if(strlen($pwd) >4){
                $code = Hint::PARAM_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $params = array(
                'user_id' => $this->requestData['user_id'] ,
                'password' =>  $this->makeHashPassword($pwd),
                'switch' => 1 ,
                'updated_at' => date('Y-m-d H:i:s')
            );

            $this->teenager->setLock($params);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    public function unlocking(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('user_id', '用户id', 'int', true, '用户id不能为空');
            $this->I('password', '密码', 'int', true, '密码不能为空');
            $this->I('switch', '开关', 'int', true, '开关不能为空');//0关闭1开启

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            if(!in_array($this->requestData['switch'] ,array(0,1))){
                $code = Hint::PARAM_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $lock_info = $this->teenager->getLockInfo($this->requestData['user_id']);
            if(empty($lock_info)){
                $code = Hint::ERR_DATA_NOT_EXIST;
                $msg = '青少年锁-'.Hint::getErrCodeMsg($code);
                break;
            }
            if($this->makeHashPassword($this->requestData['password']) != $lock_info['password']){
                $code = Hint::PASSWORD_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            if($this->requestData['switch'] == $lock_info['switch']){
                $code = Hint::PARAM_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $this->teenager->unlocking($this->requestData['user_id'] ,$this->requestData['switch']);
        } while (false);

        return compact('code', 'msg', 'data');
    }

    private function makeHashPassword($password){
        return md5($this->passwordHash.$password);
    }
}
